export class TemplateManager {
    static cache = new Map();
    
    static async loadTemplate(url, variables = {}) {
        try {
            let html;
            
            // 检查缓存
            if (this.cache.has(url)) {
                html = this.cache.get(url);
            } else {
                const response = await fetch(url);
                if (!response.ok) {
                    throw new Error(`模板加载失败: ${response.status} ${response.statusText}`);
                }
                html = await response.text();
                this.cache.set(url, html);
            }
            
            // 替换变量
            return this.interpolateTemplate(html, variables);
        } catch (error) {
            console.error('模板加载错误:', error);
            throw error;
        }
    }
    
    static interpolateTemplate(template, variables) {
        return template.replace(/\$\{(\w+)\}/g, (match, key) => {
            return variables[key] !== undefined ? variables[key] : match;
        });
    }
    
    static clearCache() {
        this.cache.clear();
    }
}